﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PoolGame2.Player
{
    public class PlayerPresence : GameComponent
    {
        public enum PlayerType
        {
            PLAYER_UNKNOWN = 0,
            PLAYER_LOCAL,
            PLAYER_LOCAL_LIVE,
            PLAYER_REMOTE_LIVE,
            PLAYER_AI,
        }

        public class PlayerGameState
        {
            float flRotation;
            public float Rotation
            {
                get { return flRotation; }
                set { flRotation = value; }
            }

            int iBallType;
            public int BallType
            {
                get { return iBallType; }
                set { iBallType = value; }
            }

            int iShotsRemaing;
            public int ShotsRemaining
            {
                get { return iShotsRemaing; }
                set { iShotsRemaing = value; }
            }

            bool bIsActivePlayer;
            public bool Active
            {
                get { return bIsActivePlayer; }
                set { bIsActivePlayer = value; }
            }
        }

        PlayerGameState playerState = new PlayerGameState();
        PlayerType playerType = PlayerType.PLAYER_UNKNOWN;
        List<PlayerAction> pendingActionList = new List<PlayerAction>();
        List<PlayerAction> outgoingActionList = new List<PlayerAction>();
        // TODO: Change
        bool isColours = false;
        bool isStripes = false;

        public List<PlayerAction> ActionList
        {
            get { return pendingActionList; }
        }
        public PlayerGameState GameState
        {
            get { return playerState; } 
        }

        public PlayerPresence(PlayerType type, PoolGame game)
            : base(game)
        {
            playerType = type;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public virtual void UpdateShot(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public Gamer GetLiveGamer()
        {
            foreach (Gamer gamer in Gamer.SignedInGamers)
            {
                if (gamer.Tag == this)
                    return gamer;
            }
            
            return null;
        }

        public SignedInGamer GetSignedInGamer()
        {
            foreach (SignedInGamer gamer in SignedInGamer.SignedInGamers)
            {
                if (gamer.Tag == this)
                    return gamer;
            }

            return null;
        }

        public NetworkGamer GetNetworkGamer()
        {
            PoolGame pGame = (PoolGame)Game;
            foreach (NetworkGamer gamer in pGame.networkManager.netSession.AllGamers)
            {
                if (gamer.Tag == this)
                    return gamer;
            }

            return null;
        }

        public void AddAction(PlayerAction act)
        {
            pendingActionList.Add(act);
            outgoingActionList.Add(act);
        }

        public void RemoveAction(PlayerAction act)
        {
            pendingActionList.Remove(act);
        }

        public void PackAction(PacketWriter output, LocalNetworkGamer gamer)
        {
            foreach (PlayerAction act in outgoingActionList)
            {
                act.Pack(output);
                gamer.SendData(output, SendDataOptions.None);
            }
            outgoingActionList.Clear();
        }

        public void setColours()
        {
            isColours = !isColours;
        }

        public bool getColours()
        {
            return isColours;
        }

        public void setStripes()
        {
            isStripes = !isStripes;
        }

        public bool getStripes()
        {
            return isStripes;
        }
    }
}
